//-----------------------------------------------------------------------------
//  Copyright (C) 2005-2008 Thomas S. Ullrich 
//
//  This file is part of the Solitaire Mahjong project.
//
//  This file may be used under the terms of the GNU General Public License.
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License.
//  
//  Author: Thomas S. Ullrich
//  Last update: November 7, 2008
//-----------------------------------------------------------------------------
#include <algorithm>
#include <iostream>
#include <QMessageBox>
#include <QHeaderView>
#include <QtDebug>
#include "smPlayedTilesList.h"
#include "smDefinitions.h"
#include "smSettings.h"
#include "smTileItem.h"

smPlayedTilesList::smPlayedTilesList(QWidget* parent, Qt::WindowFlags fl) : QDialog(parent, fl)
{
    ui.setupUi(this);
    
    ui.mTableWidget->setSortingEnabled(false); // no sorting do it manually
    setModal(false);
    ui.mTableWidget->setAutoFillBackground(true);

    ui.mTableWidget->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    ui.mTableWidget->verticalHeader()->hide();
    ui.mTableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    ui.mTableWidget->horizontalHeader()->hide();
    
    ui.mTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
    connect(ui.mAsPlayedRadioButton, SIGNAL(toggled(bool)), this, SLOT(sortingModeHasChanged()));
    
    mSortAsPlayed = ui.mAsPlayedRadioButton->isChecked(); 
    
    QPalette thePalette(ui.mTableWidget->palette());
    thePalette.setColor(QPalette::Base, smSettings::instance()->mBackgroundColor);
    thePalette.setColor(QPalette::Window, smSettings::instance()->mBackgroundColor);
    ui.mTableWidget->setPalette(thePalette);
    
    move(smSettings::instance()->mPlayedTilesWindowPosition);
    resize(smSettings::instance()->mPlayedTilesWindowSize);
    
    setModal(true);
}

smPlayedTilesList::~smPlayedTilesList()
{ 
    smSettings::instance()->mPlayedTilesWindowPosition = pos();   
    smSettings::instance()->mPlayedTilesWindowSize = size();   
}

void smPlayedTilesList::sortingModeHasChanged() 
{
    mSortAsPlayed = ui.mAsPlayedRadioButton->isChecked(); 
    layoutList();
}

void smPlayedTilesList::layoutList()
{
    ui.mTableWidget->clearContents();

    //
    //  Set same background color and tile size as on game board
    //
    QPalette thePalette(ui.mTableWidget->palette());
    thePalette.setColor(QPalette::Base, smSettings::instance()->mBackgroundColor);
    thePalette.setColor(QPalette::Window, smSettings::instance()->mBackgroundColor);
    ui.mTableWidget->setPalette(thePalette);
    if (mHistoryList.size()) {
        ui.mTableWidget->setIconSize(mHistoryList[0].first->mNormalPixmap.size()); 
    }
    
    //
    //  Layout as played, i.e. last one first always in pairs.
    //  This is essentially a dump of the history list.
    //
    if (mSortAsPlayed) {
        ui.mTableWidget->setColumnCount(2);
        ui.mTableWidget->setRowCount(mHistoryList.size());
        for (unsigned int i=0; i<mHistoryList.size(); i++) {
            ui.mTableWidget->setItem(i, 0, new QTableWidgetItem(mHistoryList[i].first->mNormalPixmap,""));
            ui.mTableWidget->setItem(i, 1, new QTableWidgetItem(mHistoryList[i].second->mNormalPixmap,""));
        }
    }
    
    //
    //  Layout such that all that belong together are shown
    //  in one row. Needs some sorting. Use vector<vector> here.
    //
    else {  
        ui.mTableWidget->setColumnCount(4);
        vector<vector<smTileItem*> > sortedList = getSortedList(mHistoryList); 
        int nrows = 0; 
        for (unsigned int i=0; i<sortedList.size(); i++) 
            if (!sortedList[i].empty()) nrows++;
        ui.mTableWidget->setRowCount(nrows);
        nrows = 0;
        for (unsigned int i=0; i<sortedList.size(); i++) {
            for (unsigned int k=0; k<sortedList[i].size(); k++) {
                ui.mTableWidget->setItem(nrows, k, new QTableWidgetItem(sortedList[i][k]->mNormalPixmap,""));
            }
            if (!sortedList[i].empty()) nrows++;
        }
    }
    
    // Check if this heals the problem on Windows
    if (mHistoryList.size()) {
        ui.mTableWidget->setIconSize(mHistoryList[0].first->mNormalPixmap.size()); 
    }
    ui.mTableWidget->resizeColumnsToContents();
    ui.mTableWidget->resizeRowsToContents();
    update();
    update();
}

vector<vector<smTileItem*> > smPlayedTilesList::getSortedList(const vector<pair<smTileItem*, smTileItem*> > &list) 
{
    vector<vector<smTileItem*> > sortedList(Dragon_White+2+1); // see smDefinitions.h
    for (unsigned int i=0; i<list.size(); i++) {
        int index = 0;
        if (list[i].first->mTileId <= Dragon_White) 
            index = list[i].first->mTileId;
        else if (list[i].first->mTileId >= Season_1 && list[i].first->mTileId <= Season_4)
            index = Dragon_White+1;
        else if (list[i].first->mTileId >= Flower_1 && list[i].first->mTileId <= Flower_4)
            index = Dragon_White+2;
        sortedList[index].push_back(list[i].first);                
        sortedList[index].push_back(list[i].second);
    }  
    return sortedList;
}

void smPlayedTilesList::sync(stack<pair<smTileItem*, smTileItem*> > historyList)
{
    //
    // Copy into vector for use in layoutList()
    //
    mHistoryList.clear();
    while (!historyList.empty()) {
        mHistoryList.push_back(historyList.top());
        historyList.pop();
    }
    layoutList();
}

void smPlayedTilesList::hideEvent(QHideEvent*)
{
    smSettings::instance()->mPlayedTilesWindowPosition = pos();   
    smSettings::instance()->mPlayedTilesWindowSize = size();   
}


void smPlayedTilesList::showEvent(QShowEvent*)
{
    move(smSettings::instance()->mPlayedTilesWindowPosition);
    resize(smSettings::instance()->mPlayedTilesWindowSize);
}